<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑导航 -->
      <XtxBread separator=">">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{ categroyName }}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxSlider width="1245px" :sliders="sliders" style="height:500px" />
    </div>
    <div class="center-container">
      <div class="box">
        <!-- 全部分类 -->
        <div class="sub-list">
          <h3>全部分类</h3>
          <ul>
            <li v-for="item in categroyAllList" :key="item.id">
              <a href="javascript:;">
                <img :src="item.picture" />
                <p>{{ item.name }}</p>
              </a>
            </li>
          </ul>
        </div>
        <!-- 分类关联商品 -->
        <div class="ref-goods" v-for="item in categroyAllList" :key="item.id">
          <div class="head">
            <h3>- {{ item.name }} -</h3>
            <div class="more">
              <XtxMore to="/" />
            </div>
          </div>
          <div class="body">
            <goodsItem v-for="good in item.goods" :goods="good" :key="good.id" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'TopCategory'
}
</script>

<script setup lang="ts">
import goodsItem from './components/goods-item.vue'
import { findTopCategory } from '@/api/category'
import { ref, onMounted } from 'vue'
import { findBanner } from '@/api/home'
import { useRoute, onBeforeRouteUpdate } from 'vue-router';
const route = useRoute()
const sliders = ref([])
const categroyAllList = ref([])
const categroyName = ref('')

const getSliders = async () => {
  const res = await findBanner()
  sliders.value = res.result
}

onMounted(() => {
  getSliders()
})

const getCategroyAllList = async (id = route.params.id) => {
  const res = await findTopCategory(id)
  categroyAllList.value = res.result.children
  categroyName.value = res.result.name
}
onMounted(() => {
  getCategroyAllList()
})
// 在路由跳转之后更新之前自动执行
// beforeEach((to,from,next)=>{})
onBeforeRouteUpdate((to) => {
  // to指代的是目标路由对象 to拿到最新的路由参数id
  // 使用最新id获取数据
  getCategroyAllList(to.params.id)
})

</script>


<style scoped lang="less">
.top-category {
  .center-container {
    background-color: #fff;
    .box {
      width: 1240px;
      margin: 0 auto;
    }
  }
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: 400;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    background-color: #fff;
  }
  .sub-list ul {
    display: flex;
    padding: 0 32px;
    flex-wrap: wrap;
    min-height: 160px;
    li {
      width: 168px;
      height: 160px;
      a {
        text-align: center;
        display: block;
        font-size: 16px;
        img {
          width: 100px;
          height: 100px;
        }
        p {
          line-height: 40px;
        }
      }
    }
  }
}
.ref-goods {
  margin-top: 20px;
  background-color: #fff;
  .head {
    position: relative;
    .more {
      position: absolute;
      right: 20px;
      top: 30px;
    }
  }
  .body {
    display: flex;
    justify-content: space-around;
    padding: 0 20px;
    .goods-item {
      display: block;
      width: 220px;
      padding: 20px 30px;
      text-align: center;
      img {
        width: 160px;
        height: 160px;
      }
      p {
        padding-top: 10px;
      }
      .name {
        font-size: 16px;
      }
      .desc {
        color: #999;
        height: 29px;
      }
      .price {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}
</style>